<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict">
  <xsl:output method="html"/>
  <xsl:param name="applicationPath"/>

  <xsl:template match="/">
    <h1>
      Metric Report:
    </h1>
    <h2>Modules</h2>
    <table border="1" cellSpacing="0" cellPadding="5" >
      <thead style="text-align: center;">
        <td Width="500">Module</td>
        <td Width="100" style="background-color: forestGreen; color:white">Maintainability Index</td>
        <td Width="100" style="background-color: darkblue; color: white;">Cyclomatic Complexity</td>
        <td Width="100" style="background-color: forestGreen; color: white;">Depth Of Inheritance</td>
        <td Width="100" style="background-color: darkblue; color:white;">Class Coupling</td>
        <td Width="100" style="background-color: forestGreen; color: white;">Lines Of Code</td>
      </thead>
    </table>
    <xsl:apply-templates select="//CodeMetricsReport" />
  </xsl:template>

  <xsl:template match="CodeMetricsReport">
    <script>
      function toggleDiv( imgId, divId )
      {
      eDiv = document.getElementById( divId );
      eImg = document.getElementById( imgId );

      if ( eDiv.style.display == "none" )
      {
      eDiv.style.display = "block";
      eImg.src = "<xsl:value-of select="$applicationPath"/>/images/arrow_minus_small.gif";
      }
      else
      {
      eDiv.style.display = "none";
      eImg.src = "<xsl:value-of select="$applicationPath"/>/images/arrow_plus_small.gif";
      }
      }
    </script>
    <xsl:apply-templates select="Targets/Target/Modules/Module" />
  </xsl:template>

  <xsl:template match="//CodeMetricsReport/Targets/Target/Modules/Module">
    <xsl:variable name="module.id" select="generate-id()" />
    <table border="1" cellSpacing="0" cellPadding="5" xmlns="http://www.w3.org/TR/xhtml1/strict">
      <tr style="text-align: center;">
        <td width="500" style="text-align: left;">
          <xsl:value-of select="@Name" />
        </td>
        <xsl:for-each select="Metrics/Metric">
          <xsl:if test="@Name='MaintainabilityIndex'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='CyclomaticComplexity'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='ClassCoupling'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='DepthOfInheritance'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='LinesOfCode'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
        </xsl:for-each>
        <td>
          <input type="image" src="{$applicationPath}/images/arrow_plus_small.gif">
            <xsl:attribute name="id">img<xsl:value-of select="$module.id"/>
            </xsl:attribute>
            <xsl:attribute name="onclick">
              javascript:toggleDiv('img<xsl:value-of select="$module.id"/>', 'divDetails<xsl:value-of select="$module.id"/>');
            </xsl:attribute>
          </input>
        </td>
      </tr>
    </table>
    
    <div style="display:none">
      <xsl:attribute name="id">divDetails<xsl:value-of select="$module.id"/>
      </xsl:attribute>
      <blockquote>
        <table border="1" cellSpacing="0" cellPadding="5" >
          <thead style="text-align: center;">
            <td width="500" >Name Space</td>
            <td width="100" style="background-color: forestGreen; color:white">Maintainability Index</td>
            <td width="100" style="background-color: darkblue; color: white;">Cyclomatic Complexity</td>
            <td width="100" style="background-color: forestGreen; color: white;">Depth Of Inheritance</td>
            <td width="100" style="background-color: darkblue; color:white;">Class Coupling</td>
            <td width="100" style="background-color: forestGreen; color: white;">Lines Of Code</td>
          </thead>
        </table>
        <xsl:apply-templates select="Namespaces/Namespace" />
      </blockquote>
    </div>
  </xsl:template>


  <xsl:template match="Namespaces/Namespace">
    <xsl:variable name="namespace.id" select="generate-id()" />
    <table border="1" cellSpacing="0" cellPadding="5" xmlns="http://www.w3.org/TR/xhtml1/strict">
      <tr style="text-align: center;">
        <td width="500" style="text-align: left;">
          <xsl:value-of select="@Name" />
        </td>
        <xsl:for-each select="Metrics/Metric">
          <xsl:if test="@Name='MaintainabilityIndex'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='CyclomaticComplexity'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='ClassCoupling'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='DepthOfInheritance'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='LinesOfCode'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
        </xsl:for-each>
        <td>
          <input type="image" src="{$applicationPath}/images/arrow_plus_small.gif">
            <xsl:attribute name="id">img<xsl:value-of select="$namespace.id"/>
            </xsl:attribute>
            <xsl:attribute name="onclick">
              javascript:toggleDiv('img<xsl:value-of select="$namespace.id"/>', 'divDetails<xsl:value-of select="$namespace.id"/>');
            </xsl:attribute>
          </input>
        </td>
      </tr>
    </table>

    <div style="display:none">
      <xsl:attribute name="id">divDetails<xsl:value-of select="$namespace.id"/>
      </xsl:attribute>
      <blockquote>
        <table border="1" cellSpacing="0" cellPadding="5" >
          <thead style="text-align: center;">
            <td width="500" >Class</td>
            <td width="100" style="background-color: forestGreen; color:white">Maintainability Index</td>
            <td width="100" style="background-color: darkblue; color: white;">Cyclomatic Complexity</td>
            <td width="100" style="background-color: forestGreen; color: white;">Depth Of Inheritance</td>
            <td width="100" style="background-color: darkblue; color:white;">Class Coupling</td>
            <td width="100" style="background-color: forestGreen; color: white;">Lines Of Code</td>
          </thead>
        </table>
        <xsl:apply-templates select="Types/Type" />
      </blockquote>
    </div>
  </xsl:template>

  <xsl:template match="Types/Type">
    <xsl:variable name="namespace.id" select="generate-id()" />
    <table border="1" cellSpacing="0" cellPadding="5" xmlns="http://www.w3.org/TR/xhtml1/strict">
      <tr style="text-align: center;">
        <td width="500" style="text-align: left;">
          <xsl:value-of select="@Name" />
        </td>
        <xsl:for-each select="Metrics/Metric">
          <xsl:if test="@Name='MaintainabilityIndex'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='CyclomaticComplexity'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='ClassCoupling'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='DepthOfInheritance'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='LinesOfCode'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
        </xsl:for-each>
        <td>
          <input type="image" src="{$applicationPath}/images/arrow_plus_small.gif">
            <xsl:attribute name="id">img<xsl:value-of select="$namespace.id"/>
            </xsl:attribute>
            <xsl:attribute name="onclick">
              javascript:toggleDiv('img<xsl:value-of select="$namespace.id"/>', 'divDetails<xsl:value-of select="$namespace.id"/>');
            </xsl:attribute>
          </input>
        </td>
      </tr>
    </table>

    <div style="display:none">
      <xsl:attribute name="id">divDetails<xsl:value-of select="$namespace.id"/>
      </xsl:attribute>
      <blockquote>
        <table border="1" cellSpacing="0" cellPadding="5" >
          <thead style="text-align: center;">
            <td width="500" >Method</td>
            <td width="100" style="background-color: forestGreen; color:white">Maintainability Index</td>
            <td width="100" style="background-color: darkblue; color: white;">Cyclomatic Complexity</td>
            <td width="100" style="background-color: forestGreen; color: white;">Class Coupling</td>
            <td width="100" style="background-color: darkblue; color:white;">Lines Of Code</td>
          </thead>
        </table>
        <xsl:apply-templates select="Members/Member" />
      </blockquote>
    </div>
  </xsl:template>

  <xsl:template match="Members/Member">
    <xsl:variable name="namespace.id" select="generate-id()" />
    <table border="1" cellSpacing="0" cellPadding="5" xmlns="http://www.w3.org/TR/xhtml1/strict">
      <tr style="text-align: center;">
        <td width="500" style="text-align: left;">
          <xsl:value-of select="@Name" />
        </td>
        <xsl:for-each select="Metrics/Metric">
          <xsl:if test="@Name='MaintainabilityIndex'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='CyclomaticComplexity'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='ClassCoupling'">
            <td width="100" style="background-color: forestGreen; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
          <xsl:if test="@Name='LinesOfCode'">
            <td width="100" style="background-color: darkblue; color: white;">
              <xsl:value-of select="@Value" />
            </td>
          </xsl:if>
        </xsl:for-each>
      </tr>
    </table>
  </xsl:template>

</xsl:stylesheet>
